package com.jiai.live.api;


import com.jiai.base.dao.UserDao;
import com.jiai.base.model.User;
import com.jiai.base.service.UserService;
import com.jiai.core.tools.RestResultGenerator;
import com.jiai.exception.ServiceException;

import com.jiai.live.dto.UserDto;
import com.jiai.live.service.LiveService;
import com.jiai.util.ComputeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by qudingyi on 2017/4/14.
 */
@RequestMapping("/api/v1/user")
@RestController
public class UserAPIController {

    @Autowired
    private UserService userService;
    @Autowired
    private LiveService liveService;


    @RequestMapping(value="/live/checkLiveUser",method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object checkLiveUser(@RequestBody User user)throws ServiceException {
        try{

                UserDto view=userService.getLiveUserInfo(user.getId());
                if(view!=null){
                    return  RestResultGenerator.genSuccessResult(view);
                }else{
                    if(!StringUtils.isEmpty(user.getBirthday())){
                        SimpleDateFormat format=new SimpleDateFormat("y" +
                                "yy/MM/dd");
                        try {
                            user.setAge(ComputeUtils.getAges(format.parse(user.getBirthday())));
                        } catch (ParseException e) {

                        }
                    }

                        user.setRegisterTime(new Date());
                        userService.save(user);
                        return  RestResultGenerator.genSuccessResult(userService.getLiveUserInfo(user.getId()));

                }
        }catch (Exception e){
                  throw  new ServiceException(e.getMessage());
        }
    }





    @RequestMapping(value ="/loadMemberView/{sex}")
    public Object loadMemberView(@PathVariable("sex") int sex)throws ServiceException {
        return null;
       // return RestResultGenerator.genSuccessResult(userService.loadMemberView(sex));
    }
}
